// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Web.Profile;
using System.Xml.Serialization;
using BlueOnionSoftware.Bloget.Utility;

namespace BlueOnionSoftware.Bloget.Providers.Data
{
    [Serializable]
    [XmlRoot("profiles", Namespace = ProfileNamespace)]
    public class ProfileCollection : Collection<Profile>
    {
        internal const string ProfileNamespace = "urn:blueonionsoftware-com:bloget:profile:data";

        public ProfileCollection()
        {
        }

        internal ProfileCollection(IList<Profile> profiles)
            : base(profiles)
        {
        }

        public static ProfileCollection Load(string fileName)
        {
            try
            {
                return Serializer.Load<ProfileCollection>(fileName, ProfileNamespace);
            }

            catch (FileNotFoundException)
            {
                return new ProfileCollection();
            }
        }

        public void Save(string fileName)
        {
            Serializer.Save<ProfileCollection>(fileName, ProfileNamespace, this);
        }

        public Collection<Profile> FindByUserName(string userName, ProfileAuthenticationOption authenticationType)
        {
            var profiles = new List<Profile>();

            foreach (var p in this)
            {
                if (string.Compare(userName, p.UserName, true, CultureInfo.CurrentCulture) == 0)
                {
                    switch (authenticationType)
                    {
                        case ProfileAuthenticationOption.All:
                            profiles.Add(p);
                            break;

                        case ProfileAuthenticationOption.Anonymous:
                            if (p.IsAnonymous)
                            {
                                profiles.Add(p);
                            }
                            break;

                        case ProfileAuthenticationOption.Authenticated:
                            if (!p.IsAnonymous)
                            {
                                profiles.Add(p);
                            }
                            break;
                    }
                }
            }

            return new Collection<Profile>(profiles);
        }

        public ProfileInfoCollection CreateProfileInfoCollection(ProfileAuthenticationOption authenticationOption)
        {
            var pic = new ProfileInfoCollection();

            foreach (var p in this)
            {
                switch (authenticationOption)
                {
                    case ProfileAuthenticationOption.All:
                        pic.Add(p.CreateProfileInfo());
                        break;

                    case ProfileAuthenticationOption.Anonymous:
                        if (p.IsAnonymous)
                        {
                            pic.Add(p.CreateProfileInfo());
                        }
                        break;

                    case ProfileAuthenticationOption.Authenticated:
                        if (!p.IsAnonymous)
                        {
                            pic.Add(p.CreateProfileInfo());
                        }
                        break;
                }
            }

            return pic;
        }
    }
}